package com.cyzy.util;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.aliyun.ocr_api20210707.models.RecognizeAllTextResponse;
import com.aliyun.tea.TeaException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.HashMap;

@Component
public class OrcUtil {


//    @Value("${aliyun.accessKeyId}")
//    private String accessKeyId;
//    @Value("${aliyun.accessKeySecret}")
//    private String accessKeySecret;
//    @Value("${aliyun.endpoint}")
//    private String endpoint;


    public com.aliyun.ocr_api20210707.Client createClient() throws Exception {
        com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
                // 必填，请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_ID。
                .setAccessKeyId("LTAI5tHqQBvBSz7rNpNPimi6")
                // 必填，请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_SECRET。
                .setAccessKeySecret("pFYm1pzvKjIl4Qb9Bsz5G6p0hDZbYk");
        // 默认公网接入地址为"ocr-api.cn-hangzhou.aliyuncs.com"，若您需要使用vpc域名访问，请确保您的ecs建立在杭州region，vpc接入地址为"ocr-api-vpc.cn-hangzhou.aliyuncs.com"
        config.endpoint = "ocr-api.cn-hangzhou.aliyuncs.com";
        return new com.aliyun.ocr_api20210707.Client(config);
    }


    public HashMap<String,String> recognition(String path) throws Exception {

        com.aliyun.ocr_api20210707.Client client = null;
        client = createClient();
        java.io.InputStream bodyStream = null;
        try {
            bodyStream = com.aliyun.darabonba.stream.Client.readFromFilePath(path);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        com.aliyun.ocr_api20210707.models.RecognizeIdcardRequest recognizeIdcardRequest = new com.aliyun.ocr_api20210707.models.RecognizeIdcardRequest().setBody(bodyStream);
        com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
        try {
            com.aliyun.ocr_api20210707.models.RecognizeIdcardResponse resp = client.recognizeIdcardWithOptions(recognizeIdcardRequest,runtime);
            JSONObject data = JSON.parseObject(resp.getBody().getData())
                    .getJSONObject("data")
                    .getJSONObject("face")
                    .getJSONObject("data");
            HashMap<String,String> hashMap = new HashMap<>();

            hashMap.put("address",data.getString("address"));
            hashMap.put("ethnicity",data.getString("ethnicity"));
            hashMap.put("sex",data.getString("sex"));
            hashMap.put("name",data.getString("name"));
            hashMap.put("idNumber",data.getString("idNumber"));
            hashMap.put("birth",data.getString("idNumber"));
            hashMap.put("birthDate",data.getString("birthDate"));

            return hashMap;


        } catch (TeaException error) {
            // 此处仅做打印展示，请谨慎对待异常处理，在工程项目中切勿直接忽略异常。
            // 错误 message
            System.out.println(error.getMessage());
            // 诊断地址
            System.out.println(error.getData().get("Recommend"));
            com.aliyun.teautil.Common.assertAsString(error.message);
        } catch (Exception _error) {
            TeaException error = new TeaException(_error.getMessage(), _error);
            // 此处仅做打印展示，请谨慎对待异常处理，在工程项目中切勿直接忽略异常。
            // 错误 message
            System.out.println(error.getMessage());
            // 诊断地址
            System.out.println(error.getData().get("Recommend"));
            com.aliyun.teautil.Common.assertAsString(error.message);
        }
        return null;
    }

}
